package com.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.entity.MerchantEntity;
import com.platform.entity.MerchantInfoEntity;
import com.platform.entity.MerchantInfomationEntity;
import com.platform.service.MerchantInfoService;
import com.platform.service.MerchantInfomationService;
import com.platform.service.MerchantService;
import com.platform.utils.BeanUtils;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *
 * 商城配置:商户通过列表
 *
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-24 16:26:25
 */
@RestController
@RequestMapping("merchant")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MerchantInfomationService merchantInfomationService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("merchant:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<MerchantEntity> merchantList = merchantService.queryList(query);
        int total = merchantService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(merchantList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("merchant:info")
    public R info(@PathVariable("id") Integer id) {
        MerchantEntity merchant = merchantService.queryObject(id);
        Map<String,Object> xmlBean = BeanUtils.bean2Map(merchant,true);
        MerchantInfomationEntity merchantInfoEntity = merchantInfomationService.queryObjectByMid(merchant.getId());
        if(merchantInfoEntity!=null){
            xmlBean.put("logo",merchantInfoEntity.getLogo());
        }else{
            xmlBean.put("logo","");
        }
        return R.ok().put("merchant", xmlBean);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   //@RequiresPermissions("merchant:save")
    public R save(@RequestBody MerchantEntity merchant) {
        merchantService.save(merchant);

        return R.ok();
    }

    @RequestMapping("/saveObject")
    public R saveObject(@RequestBody Map<String,Object> param) {
//        String logo =  param.get("logo").toString();
//        System.out.println(JSONObject.toJSONString(param));
        MerchantEntity merchantEntity = new MerchantEntity();
        if(param.containsKey("userId")!=false){
            merchantEntity.setUserId(Long.valueOf(param.get("userId").toString()));
        }else{
            return R.error("请输入平台用户编号!");
        }
        if(param.containsKey("ddPhone")!=false){
            merchantEntity.setDdPhone(param.get("ddPhone").toString());
        }else{
            return R.error("请输入平台用户手机!");
        }
        if(param.containsKey("realname")!=false){
            merchantEntity.setRealname(param.get("realname").toString());
        }else{
            return R.error("请输入商户姓名!");
        }
        if(param.containsKey("name")!=false){
            merchantEntity.setName(param.get("name").toString());
        }else{
            return R.error("请输入商户名称!");
        }
        if(param.containsKey("phone")!=false){
            merchantEntity.setPhone(param.get("phone").toString());
        }else{
            return R.error("请输入商户手机号!");
        }
        if(param.containsKey("address")!=false){
            merchantEntity.setAddress(param.get("address").toString());
        }else{
            return R.error("请输入商户地址!");
        }
        if(param.containsKey("idcard")!=false){
            merchantEntity.setIdcard(param.get("idcard").toString());
        }else{
            return R.error("请输入法人身份证号!");
        }


        if(param.containsKey("value")!=false){
            merchantEntity.setValue(param.get("value").toString());
        }else{
            merchantEntity.setValue("");
        }
        if(param.containsKey("value1")!=false){
            merchantEntity.setValue(param.get("value1").toString());
        }else{
            merchantEntity.setValue("");
        }
        if(param.containsKey("license")!=false){
            merchantEntity.setLicense(param.get("license").toString());
        }else{
            merchantEntity.setLicense("");
        }
        if(param.containsKey("openLicence")!=false){
            merchantEntity.setOpenLicence(param.get("openLicence").toString());
        }else{
            merchantEntity.setOpenLicence("");
        }
        if(param.containsKey("institution")!=false){
            merchantEntity.setInstitution(param.get("institution").toString());
        }else{
            merchantEntity.setInstitution("");
        }
        if(param.containsKey("value1")!=false){
            merchantEntity.setValue1(param.get("value1").toString());
        }else{
            merchantEntity.setValue1("");
        }
        if(param.containsKey("value2")!=false){
            merchantEntity.setValue2(param.get("value2").toString());
        }else{
            merchantEntity.setValue2("");
        }
        if(param.containsKey("value5")!=false){
            merchantEntity.setValue5(param.get("value5").toString());
        }else{
            merchantEntity.setValue5("");
        }
        if(param.containsKey("value3")!=false){
            merchantEntity.setValue3(param.get("value3").toString());
        }else{
            merchantEntity.setValue3("");
        }
        if(param.containsKey("value4")!=false){
            merchantEntity.setValue4(param.get("value4").toString());
        }else{
            merchantEntity.setValue4("");
        }
        if(param.containsKey("statu")!=false){
            merchantEntity.setStatu(Integer.parseInt(param.get("statu").toString()));
        }else{
            merchantEntity.setStatu(1);
        }

        if(merchantService.save(merchantEntity)<=0){
            return R.error();
        }
        MerchantEntity queryMerchant = merchantService.queryObjectByUid(Integer.parseInt(param.get("userId").toString()));
        MerchantInfomationEntity merchantInfomationEntity = new MerchantInfomationEntity();
        merchantInfomationEntity.setMid(queryMerchant.getId().longValue());
        merchantInfomationEntity.setVersion(1);
        merchantInfomationEntity.setAmount(new BigDecimal(0));
        merchantInfomationEntity.setFrozen(new BigDecimal(0));
        merchantInfomationEntity.setUserId(Long.valueOf(param.get("userId").toString()));//保存用户id
        if(param.containsKey("logo")!=false){
            merchantInfomationEntity.setLogo(param.get("logo").toString());
        }else{
            merchantInfomationEntity.setLogo("");
        }
        merchantInfomationEntity.setStatu(queryMerchant.getStatu());
        merchantInfomationEntity.setName(queryMerchant.getName());
        merchantInfomationEntity.setInstitution(queryMerchant.getInstitution());

        if(merchantInfomationService.save(merchantInfomationEntity)<=0){
            return R.error();
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updateObject")
    //@RequiresPermissions("merchant:update")
    public R updateObject(@RequestBody  Map<String,Object> param) {
//        String logo = param.get("logo").toString();

        MerchantEntity merchantEntity = new MerchantEntity();
        if(param.containsKey("userId")!=false){
            merchantEntity.setId(Integer.parseInt(param.get("userId").toString()));
        }else{
            return R.error("请输入平台用户编号!");
        }
        if(param.containsKey("ddPhone")!=false){
            merchantEntity.setDdPhone(param.get("ddPhone").toString());
        }else{
            return R.error("请输入平台用户手机!");
        }
        if(param.containsKey("realname")!=false){
            merchantEntity.setRealname(param.get("realname").toString());
        }else{
            return R.error("请输入商户姓名!");
        }
        if(param.containsKey("name")!=false){
            merchantEntity.setName(param.get("name").toString());
        }else{
            return R.error("请输入商户名称!");
        }
        if(param.containsKey("phone")!=false){
            merchantEntity.setPhone(param.get("phone").toString());
        }else{
            return R.error("请输入商户手机号!");
        }
        if(param.containsKey("address")!=false){
            merchantEntity.setAddress(param.get("address").toString());
        }else{
            return R.error("请输入商户地址!");
        }
        if(param.containsKey("idcard")!=false){
            merchantEntity.setIdcard(param.get("idcard").toString());
        }else{
            return R.error("请输入法人身份证号!");
        }
        if(param.containsKey("id")!=false){
            merchantEntity.setId(Integer.parseInt(param.get("id").toString()));
        }else{
            return R.error("信息异常!");
        }



        if(param.containsKey("value")!=false){
            merchantEntity.setValue(param.get("value").toString());
        }else{
            merchantEntity.setValue("");
        }
        if(param.containsKey("value1")!=false){
            merchantEntity.setValue1(String.valueOf(param.get("value1")));
        }else{
            merchantEntity.setValue1("");
        }
        if(param.containsKey("license")!=false){
            merchantEntity.setLicense(param.get("license").toString());
        }else{
            merchantEntity.setLicense("");
        }
        if(param.containsKey("openLicence")!=false){
            merchantEntity.setOpenLicence(param.get("openLicence").toString());
        }else{
            merchantEntity.setOpenLicence("");
        }
        if(param.containsKey("institution")!=false){
            merchantEntity.setInstitution(param.get("institution").toString());
        }else{
            merchantEntity.setInstitution("");
        }
        if(param.containsKey("value2")!=false){
            merchantEntity.setValue2(String.valueOf(param.get("value2")));
        }else{
            merchantEntity.setValue2("");
        }
        if(param.containsKey("value5")!=false){
            merchantEntity.setValue5(String.valueOf(param.get("value5")));
        }else{
            merchantEntity.setValue5("");
        }
        if(param.containsKey("value3")!=false){
            merchantEntity.setValue3(String.valueOf(param.get("value3")));
        }else{
            merchantEntity.setValue3("");
        }
        if(param.containsKey("value4")!=false){
            merchantEntity.setValue4(String.valueOf(param.get("value4")));
        }else{
            merchantEntity.setValue4("");
        }
        if(param.containsKey("statu")!=false){
            merchantEntity.setStatu(Integer.parseInt(String.valueOf(param.get("statu"))));
        }else{
            merchantEntity.setStatu(1);
        }
        if(merchantService.update(merchantEntity)<=0){
            return R.error();
        }

        MerchantInfomationEntity merchantInfomationEntity = merchantInfomationService.queryObjectByMid(Integer.parseInt(param.get("id").toString()));
        if(merchantInfomationEntity==null){
            MerchantEntity queryMerchant = merchantService.queryObjectByUid(Integer.parseInt(param.get("userId").toString()));
            MerchantInfomationEntity merchantInfomationEntity1 = new MerchantInfomationEntity();
            merchantInfomationEntity1.setMid(queryMerchant.getId().longValue());
            merchantInfomationEntity1.setVersion(1);
            merchantInfomationEntity1.setAmount(new BigDecimal(0));
            merchantInfomationEntity1.setFrozen(new BigDecimal(0));
            if(param.containsKey("logo")!=false){
                merchantInfomationEntity1.setLogo(param.get("logo").toString());
            }else{
                merchantInfomationEntity1.setLogo("");
            }
            merchantInfomationEntity1.setStatu(queryMerchant.getStatu());
            merchantInfomationEntity1.setName(queryMerchant.getName());
            merchantInfomationEntity1.setInstitution(queryMerchant.getInstitution());
            if(merchantInfomationService.save(merchantInfomationEntity1)<=0){
                return R.error();
            }
        }else{
            if(param.containsKey("logo")!=false){
                merchantInfomationEntity.setLogo(param.get("logo").toString());
            }else{
                merchantInfomationEntity.setLogo("");
            }
            merchantInfomationEntity.setStatu(merchantEntity.getStatu());
            //merchantInfomationEntity.setStatu(Integer.parseInt(param.get("statu").toString()));
            if(param.containsKey("institution")!=false){
                merchantInfomationEntity.setInstitution(param.get("institution").toString());
            }else{
                merchantInfomationEntity.setInstitution("");
            }
            if(merchantInfomationService.update(merchantInfomationEntity)<=0){
                return R.error();
            }
        }


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("merchant:update")
    public R update(@RequestBody MerchantEntity merchant) {
        merchantService.update(merchant);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("merchant:delete")
    public R delete(@RequestBody Integer[] ids) {
        merchantService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MerchantEntity> list = merchantService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 查询所有列表
     * @return
     */
    @RequestMapping("/queryMerchantList")
    public R queryMerchantlist(){
        List<MerchantEntity> list = merchantService.queryMerchantList();
        return R.ok().put("list",list);
    }
}
